#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int MAXN = 2e5+10;
const int mod = 1e9+7;

int n,cnt = 1,x,tot = 0,fa[MAXN],s = 0;
struct edge{int v,c;};
vector<edge>T[MAXN];
ll ans = 1;
bool vis[MAXN<<1],v[MAXN];

void calc(int x,int y){
	while(x != y) x = fa[x],++tot,--s;
}

void dfs(int x){
	v[x] = 1;
	for(auto d : T[x]){
		int y = d.v,num = d.c; if(vis[num]) continue;
		vis[num] = vis[num^1] = 1;
		if(v[y]) calc(x,y);
		else fa[y] = x,s++,dfs(y);
	}
}

ll qp(ll x,int t){ll res=1;for(;t;t>>=1,x=x*x%mod)if(t&1)res=res*x%mod;return res;}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in; for(int i = 1;i <= n;i++) x = in,T[i].pb((edge){x,++cnt}),T[x].pb((edge){i,++cnt});
	for(int i = 1;i <= n;i++)
		if(!v[i]) {
			tot = 0;s = 1;dfs(i);
			ans = ans * (qp(2,tot) - 1) % mod * qp(2,s) % mod;
		}
	printf("%lld\n",ans);
	return 0;
}

